#include "adjustwindow.h"

AdjustWindow::AdjustWindow(QWidget *parent)
    : QMainWindow{parent}
{
    this -> setWindowTitle("基础选择界面");
    this -> showFullScreen();
    this -> setWindowFlag(Qt::FramelessWindowHint);

    w = new MainWindow;
    c = new ColorChoose;

    QPalette palette(QPalette::Window,QColorConstants::Svg::lightgreen);
    this -> setPalette(palette);

    QWidget * adjustMainWidget = new QWidget(this);
    setCentralWidget(adjustMainWidget);

    playerNumberBox = new QSpinBox(adjustMainWidget);
    boardWidthSizeBox = new QSpinBox(adjustMainWidget);
    boardHeightSizeBox = new QSpinBox(adjustMainWidget);
    winCondition = new QSpinBox(adjustMainWidget);

    playerNumberBox -> setMinimum(2);
    playerNumberBox -> setMaximum(100);
    playerNumberBox -> setSingleStep(1);
    playerNumberBox -> setValue(2);
    playerNumberBox -> setMinimumSize(200,50);
    playerNumberBox -> setStyleSheet("QSpinBox {font-size:16px; padding:5px; border:1px solid #ccc; border-radius:5px;}");

    boardHeightSizeBox -> setMinimum(1);
    boardHeightSizeBox -> setMaximum(1000);
    boardHeightSizeBox -> setSingleStep(1);
    boardHeightSizeBox -> setValue(25);
    boardHeightSizeBox -> setMinimumSize(200,50);
    boardHeightSizeBox -> setStyleSheet("QSpinBox {font-size:16px; padding:5px; border:1px solid #ccc; border-radius:5px;}");

    boardWidthSizeBox -> setMinimum(1);
    boardWidthSizeBox -> setMaximum(1000);
    boardWidthSizeBox -> setSingleStep(1);
    boardWidthSizeBox -> setValue(25);
    boardWidthSizeBox -> setMinimumSize(200,50);
    boardWidthSizeBox -> setStyleSheet("QSpinBox {font-size:16px; padding:5px; border:1px solid #ccc; border-radius:5px;}");

    winCondition -> setMinimum(1);
    winCondition -> setMaximum(100);
    winCondition -> setSingleStep(1);
    winCondition -> setValue(5);
    winCondition -> setMinimumSize(200,50);
    winCondition -> setStyleSheet("QSpinBox {font-size:16px; padding:5px; border:1px solid #ccc; border-radius:5px;}");

    QVBoxLayout * layout = new QVBoxLayout(adjustMainWidget);
    layout -> setAlignment(Qt::AlignCenter);
    layout -> setSpacing(30);

    QHBoxLayout * boardWidthSizeBoxLayout = new QHBoxLayout();
    boardWidthSizeBoxLabel = new QLabel("棋盘宽度：");
    boardWidthSizeBoxLabel -> setStyleSheet("QLabel {font-size:16px;}");
    boardWidthSizeBoxLayout -> addWidget(boardWidthSizeBoxLabel);
    boardWidthSizeBoxLayout -> addWidget(boardWidthSizeBox);
    boardWidthSizeBoxLayout -> setSpacing(10);

    QHBoxLayout * boardHeightSizeBoxLayout = new QHBoxLayout();
    boardHeightSizeBoxLabel = new QLabel("棋盘高度：");
    boardHeightSizeBoxLabel -> setStyleSheet("QLabel {font-size:16px;}");
    boardHeightSizeBoxLayout -> addWidget(boardHeightSizeBoxLabel);
    boardHeightSizeBoxLayout -> addWidget(boardHeightSizeBox);
    boardHeightSizeBoxLayout -> setSpacing(10);

    QHBoxLayout * playerNumberBoxLayout = new QHBoxLayout();
    playerNumberBoxLabel = new QLabel("游戏人数：");
    playerNumberBoxLabel -> setStyleSheet("QLabel {font-size:16px;}");
    playerNumberBoxLayout -> addWidget(playerNumberBoxLabel);
    playerNumberBoxLayout -> addWidget(playerNumberBox);
    playerNumberBoxLayout -> setSpacing(10);

    QHBoxLayout *winConditionLayout = new QHBoxLayout();
    winConditionLabel = new QLabel("获胜所需子数：");
    winConditionLabel -> setStyleSheet("QLabel {font-size:16px;}");
    winConditionLayout -> addWidget(winConditionLabel);
    winConditionLayout -> addWidget(winCondition);
    winConditionLayout -> setSpacing(10);

    layout -> addLayout(playerNumberBoxLayout);
    layout -> addLayout(boardWidthSizeBoxLayout);
    layout -> addLayout(boardHeightSizeBoxLayout);
    layout -> addLayout(winConditionLayout);

    confirmButton = new QPushButton("确认",this);
    layout -> addWidget(confirmButton);
    confirmButton -> setStyleSheet("color:white;"
                                 "background-color:lightblue;"
                                 "padding:3px 6px;"
                                 "font-size:30px;"
                                 "border-radius:5px;");
    confirmButton -> setCursor(Qt::PointingHandCursor);

    closeButton = new QPushButton("关闭",this);
    layout -> addWidget(closeButton);
    closeButton -> setStyleSheet("color:white;"
                               "background-color:lightblue;"
                               "padding:3px 6px;"
                               "font-size:30px;"
                               "border-radius:5px;");

    closeButton -> setCursor(Qt::PointingHandCursor);


    connect(c,&ColorChoose::closeSignal,this,&AdjustWindow::ActSignalFromMainClose);
    connect(confirmButton,&QPushButton::clicked,this,&AdjustWindow::onConfirmButtonClicked);
    connect(closeButton,&QPushButton::clicked,this,&AdjustWindow::onCloseButtonClicked);
    connect(w,&MainWindow::signalShowAdjust,this,&AdjustWindow::ActSignalFromOverMessage);
    connect(w,&MainWindow::signalCloseFormMain,this,&AdjustWindow::ActSignalFromMainClose);
    connect(boardHeightSizeBox,SIGNAL(valueChanged(int)),this,SLOT(sendBoardHeightSizeBoxValueChangedSignal(int)));
    connect(playerNumberBox,SIGNAL(valueChanged(int)),this,SLOT(sendPlayerNumberBoxValueChangedSignal(int)));
    connect(boardWidthSizeBox,SIGNAL(valueChanged(int)),this,SLOT(sendBoardWidthSizeBoxValueChangedSignal(int)));
    connect(this,SIGNAL(signalBoardHeightSizeBoxValueChanged(int)),w,SLOT(changeBoardHeightSizeValue(int)));
    connect(this,SIGNAL(signalBoardWidthSizeBoxValueChanged(int)),w,SLOT(changeBoardWidthSizeValue(int)));
    connect(this,SIGNAL(signalPlayerNumberBoxValueChanged(int)),w,SLOT(changeTotalPlayerValue(int)));
    connect(this,SIGNAL(sendIfConfirm(bool)),w,SLOT(ifConfirmAct(bool)));
    connect(this,SIGNAL(signalPlayerNumberBoxValueChanged(int)),c,SLOT(getTotalPlayer(int)));
    // connect(this,SIGNAL(sendIfConfirm(bool)),c,SLOT(void ifConfirmAct(bool)));
    connect(this,SIGNAL(sendMainWindow(MainWindow*)),c,SLOT(getMainWindow(MainWindow*)));
    connect(this,SIGNAL(sendCondition(int)),w,SLOT(recieveWinCondition(int)));
    connect(winCondition,SIGNAL(valueChanged(int)),this,SLOT(sendConditionValueChangedSignal(int)));
    // connect(this,SIGNAL(sendIfConfirm(bool)),c,SLOT(ifConfirm(bool)));
}
AdjustWindow::~AdjustWindow(){
    delete playerNumberBox;
    delete boardWidthSizeBox;
    delete confirmButton;
    delete closeButton;
    delete w;
    delete boardHeightSizeBox;
    delete playerNumberBoxLabel;
    delete boardHeightSizeBoxLabel;
    delete boardWidthSizeBoxLabel;
    delete c;
    delete winConditionLabel;
    delete winCondition;
}
void AdjustWindow::onConfirmButtonClicked(){
    this -> hide();
    emit sendIfConfirm(true);
    emit sendMainWindow(w);
    //w -> show();
    c -> show();
}
void AdjustWindow::ActSignalFromOverMessage(){
    this -> show();
}
void AdjustWindow::ActSignalFromMainClose(){
    this -> close();
    emit signalCloseFromAdjust();
}
void AdjustWindow::onCloseButtonClicked(){
    ActSignalFromMainClose();
}
void AdjustWindow::sendBoardHeightSizeBoxValueChangedSignal(int value){
    emit signalBoardHeightSizeBoxValueChanged(value);
    // qDebug() << "BoardHeightSizeBoxValue received from SIGNAL is: " << value << Qt::endl;
    // qDebug() << "BoardHeightSizeBoxValue send value is: " << boardHeightSizeBox -> value() << Qt::endl;
}
void AdjustWindow::sendPlayerNumberBoxValueChangedSignal(int value){
    emit signalPlayerNumberBoxValueChanged(value);
    // qDebug() << "PlayerNumberBoxValue received from SIGNAL is: " << value << Qt::endl;
    // qDebug() << "PlayerNumberBoxValue send value is: " << playerNumberBox -> value() << Qt::endl;
}
void AdjustWindow::sendBoardWidthSizeBoxValueChangedSignal(int value){
    emit signalBoardWidthSizeBoxValueChanged(value);
    // qDebug() << "BoardWidthSizeBoxValue received from SIGNAL is: " << value << Qt::endl;
    // qDebug() << "BoardWidthSizeBoxValue send value is: " << boardWidthSizeBox -> value() << Qt::endl;
}
void AdjustWindow::sendConditionValueChangedSignal(int value){
    emit sendCondition(value);
}
